﻿using System;
using System.Text;
using System.Web;

namespace Acc.Lib
{
    public static class CookiesFunc
    {
        private static readonly Encoding Encode = Encoding.GetEncoding("GB2312");

        public static void WriteUserCookie(UserInfo value)
        {
            var cusr = new HttpCookie("userinfo") {Value = value.Serialize().UrlEncoding(Encode)};
            HttpContext.Current.Response.Cookies.Add(cusr);
        }

        public static UserInfo GetUserCookie()
        {
            var cookie = HttpContext.Current.Request.Cookies["userinfo"];
            if (cookie == null || String.IsNullOrEmpty(cookie.Value)) return null;

            return cookie.Value.UrlDecode(Encode).Deserialize<UserInfo>();
        }
    }
}